/**
 * @file       MainMenuPanel.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Feb 3, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include "MainMenuPanel.h"
#include "MenuButton.h"
#include "IEventListener.h"
#include "KeyboardListener.h"
#include "errorcode.h"
#include "Rect.h"
#include <cstdio>
#include "BaseSystemSettingsPanel.h"
#include "NetworkModeSettingsPanel.h"
#include "NatOrRouterModeSettingsPanel.h"
#include "ManagementIPSettingsPanel.h"

using namespace NS_LCD_AFW;
CMainMenuPanel::CMainMenuPanel()
{
}

CMainMenuPanel::~CMainMenuPanel()
{
}

int CMainMenuPanel::onCreate()
{
	//TODO: init the memuitem here
	int iRet = ERR_SUCCESS;
	bool isNatMode = true; //TODO: trust here
	CMenuButton *pMenuItem = NULL;
	IEventListener *pListener = NULL;

	OBJ_NEW(CMenuButton, pMenuItem);
	if (ERR_SUCCESS
			!= (iRet = pMenuItem->create("BaseSystemSettingItem",
					CRect(1, 1, LCD_WIDTH, 1), this)))
	{
		goto err_proc;
	}
	pListener = new CKeyboardListener(this, ENTER_KEY_PRESSED_EVENT_ID,
			(AFW_EventCallBack) (&CMainMenuPanel::onBaseSystemSettingBtnClick));
	if (ERR_SUCCESS != (iRet = pMenuItem->addEventListener(pListener)))
	{
		goto err_proc;
	}
	pMenuItem->setText("1. Base System Settings");
	this->add(pMenuItem);

	OBJ_NEW(CMenuButton, pMenuItem);
	if (ERR_SUCCESS
			!= (iRet = pMenuItem->create("NetworkModeSettingItem",
					CRect(1, 2, LCD_WIDTH, 1), this)))
	{
		goto err_proc;
	}
	pListener =
			new CKeyboardListener(
					this,
					ENTER_KEY_PRESSED_EVENT_ID,
					(AFW_EventCallBack) (&CMainMenuPanel::onNetworkModeSettingsBtnClick));
	if (ERR_SUCCESS != (iRet = pMenuItem->addEventListener(pListener)))
	{
		goto err_proc;
	}
	pMenuItem->setText("2. Network Mode Settings");
	this->add(pMenuItem);

	OBJ_NEW(CMenuButton, pMenuItem);
	if (isNatMode)
	{
		if (ERR_SUCCESS
				!= (iRet = pMenuItem->create("NatOrRoutermOdeSettingItem",
						CRect(1, 3, LCD_WIDTH, 1), this)))
		{
			goto err_proc;
		}
		pListener =
				new CKeyboardListener(
						this,
						ENTER_KEY_PRESSED_EVENT_ID,
						(AFW_EventCallBack) (&CMainMenuPanel::onNatOrRouterModeSettingsBtnClick));
		if (ERR_SUCCESS != (iRet = pMenuItem->addEventListener(pListener)))
		{
			goto err_proc;
		}
		pMenuItem->setText("3. NAT/Router Mode Settings");
		this->add(pMenuItem);
	}
	else
	{
		if (ERR_SUCCESS
				!= (iRet = pMenuItem->create("ManagementIpSettingItem",
						CRect(1, 3, LCD_WIDTH, 1), this)))
		{
			goto err_proc;
		}
		pListener =
				new CKeyboardListener(
						this,
						ENTER_KEY_PRESSED_EVENT_ID,
						(AFW_EventCallBack) (&CMainMenuPanel::onNatOrRouterModeSettingsBtnClick));
		if (ERR_SUCCESS != (iRet = pMenuItem->addEventListener(pListener)))
		{
			goto err_proc;
		}
		pMenuItem->setText("4. Management  IP Setting");
		this->add(pMenuItem);
	}

	return CPanel::onCreate();

	err_proc: if (NULL != pListener)
	{
		pListener->destory();
	}
	//OBJ_DELETE(pMenuItem);
	if (NULL != pMenuItem)
	{
		pMenuItem->destory();
		pMenuItem = NULL;
	}
	return iRet;

}

int CMainMenuPanel::onEnterKeyPress()
{
	//Change to the sub menu or configuration items screen of  the selected item
	return CPanel::onEnterKeyPress();
}

int CMainMenuPanel::onEscKeyPress()
{
	//Return To Main Screen Panel
	return CPanel::onEscKeyPress();
}

int CMainMenuPanel::onUpKeyPress()
{
	//Switch to prev Menu Item
	return CPanel::onUpKeyPress();
}

int CMainMenuPanel::onDownKeyPress()
{
	// Switch to next Menu Item
	return CPanel::onDownKeyPress();
}

void CMainMenuPanel::onBaseSystemSettingBtnClick(int id, int param)
{
	// Switch to Base System Settings Panel
	int iRet = ERR_SUCCESS;
	CBaseSystemSettingsPanel *pBaseSystemSettingsPanel = NULL;
	OBJ_NEW(CBaseSystemSettingsPanel, pBaseSystemSettingsPanel);
	if (NULL == pBaseSystemSettingsPanel
			|| ERR_SUCCESS
					!= (iRet = pBaseSystemSettingsPanel->create(
							"BaseSystemSettingsPanel", DEFAULT_RECT, this)))
	{
		OBJ_DELETE(pBaseSystemSettingsPanel);
		return;
	}

	pBaseSystemSettingsPanel->show();
}

void CMainMenuPanel::onNetworkModeSettingsBtnClick(int id, int param)
{
	// Switch to Network Mode Settings Panel
	int iRet = ERR_SUCCESS;
	CNetworkModeSettingsPanel *pPanel = NULL;

	OBJ_NEW(CNetworkModeSettingsPanel, pPanel);
	if (NULL == pPanel
			|| ERR_SUCCESS
					!= (iRet = pPanel->create("NetworkModeSettingsPanel",
							DEFAULT_RECT, this)))
	{
		OBJ_DELETE(pPanel);
		return;
	}

	pPanel->show();
}

void CMainMenuPanel::onNatOrRouterModeSettingsBtnClick(int id, int param)
{
	// Switch to  NatOrRouterMode  Settings Panel
	int iRet = ERR_SUCCESS;
	CNatOrRouterModeSettingsPanel *pPanel = NULL;

	OBJ_NEW(CNatOrRouterModeSettingsPanel, pPanel);
	if (NULL == pPanel
			|| ERR_SUCCESS
					!= (iRet = pPanel->create("NatOrRouterModeSettingsPanel",
							DEFAULT_RECT, this)))
	{
		OBJ_DELETE(pPanel);
		return;
	}

	pPanel->show();
}

void CMainMenuPanel::onManagementIPSettingsBtnClick(int id, int param)
{
	//Switch to ManagementIP Settings Panel
	int iRet = ERR_SUCCESS;
	CManagementIPSettingsPanel *pPanel = NULL;

	OBJ_NEW(CManagementIPSettingsPanel, pPanel);
	if (NULL == pPanel
			|| ERR_SUCCESS
					!= (iRet = pPanel->create("ManagementIPSettingsPanel",
							DEFAULT_RECT, this)))
	{
		OBJ_DELETE(pPanel);
		return;
	}

	pPanel->show();
}

int CMainMenuPanel::onClose()
{
	return ERR_SUCCESS;
}

